const app = getApp();
var util = require('../../utils/util')
var base = new util.Base64()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowCertificate:true,
    memberSignin:'',
    navH: app.data.navHeight,
    cardsList: [],
    cardsListpage:1,
    loading: true,
    /* 支付相关字段 */
    showContact: false,
    member_card_id: ''
  },
  showCertificatec(){
    wx.$.recommend_id = 0;
    this.data.cardsdataindex = '';
    this.setData({
      memberSignin:'',
      sharefriend:false,
      isShowCertificate:true,
    })
    this.onLoad()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.data.jsonPage) {
      var nvabarData = {
        title: '我的会员卡',
        bgclase: false,
        showCapsule: true,
        textclase: false,
        texttype: false
      };
      this.setData({
        nvabarData: nvabarData,
      });
    } else {
      var left = this;
      app.getjsonPage = res => {
        var nvabarData = {
          title: '我的会员卡',
          bgclase: false,
          showCapsule: true,
          textclase: false,
          texttype: false
        };
        left.setData({
          nvabarData: nvabarData,
        });
      };
    }
    this.getObtainCard()
    this.getList()
  },
  buthis() {
    var _that = this
    const system = wx.getSystemInfoSync().system;
    if (false) {//system.indexOf("iOS") != -1
      var buyInfo = {
        goods_id: 3,
        goods_type: 'buy',
        quantity: 1
      };
      buyInfo = JSON.stringify(buyInfo);
      this.setData({
        buyInfo: buyInfo,
        showContact: true,
        ios: true
      })
    } else {
      wx.$.fetch(`api/order/checkout`, {
        method: 'post',
        data: {
          card_id: this.recommend[0].id,
        }
      }).then(res => {
        console.log(res)
        if (res.data.code == 200) {
          wx.requestPayment({
            'timeStamp': res.data.data.timeStamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            'success': function (res) {
              setTimeout(function () {
                wx.reLaunch({
                  url: "/pages/home/home"
                });
              }, 200)
            }
          })
        }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 1000
            })
        }
      })
    }
  },
  // btnUse(){
  //   console.log("开启自习室或购买卡")
  //   wx.$.fetch('api/member/has-available-card',{
  //     hideLoading: false,
  //     method: 'get',
  //   }).then(res=>{
  //     if(res.data.code == 200){
  //       if (res.data.data == 1){
  //         this.Sweepcode()
  //       }else{
  //         this.purchaseVip();
  //       }
  //     }else{
  //       wx.showModal({
  //         title: "提示",
  //         content: res.data.msg,
  //         confirmColor: "#50B23F",
  //         showCancel: false
  //       });
  //     }
  //   })
  // },
  Sweepcode() {
    let left = this
    wx.scanCode({
      success(res) {
        let path = res.path
          ? res.path
          : decodeURIComponent(base.decode(res.rawData));
        let data = util.getQueryObject(path);
        if (data.Signintype){
          left.getMemberSignin()
        } else {
          wx.showToast({
            title: '请扫签到二维码',
            icon: 'none',
            mask: true,
            duration: 5000,
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '扫码失败请重新扫码',
          icon: 'none',
          mask: true,
          duration: 4000,
        })
      },
    })
  },
   /* 签到 */
   getMemberSignin(){
    let left = this;
    // let member_card_id = this.data.cardsdataindex ? this.data.cardsdataindex:'';
    wx.$.fetch(`api/member/signin`, {
      method: 'POST',
      data: {
        type: 1,
        member_card_id: this.data.member_card_id
      },
    }).then(res => {
      console.log(res)
      if (res.data.code == 200) {
        left.setData({
          isShowCertificate: false,
          memberSignin: res.data.code
        })
        left.data.cardsdataindex = '';
      } else if (res.data.code == 30007) {
        left.setData({
          isShowCertificate: false,
          memberSignin: res.data.code
        })
      } else if (res.data.code == 30006) {
        left.setData({
          isShowCertificate: false,
          memberSignin: res.data.code,
          cardsdata: res.data.data.cards,
          cardsdataindex: res.data.data.cards[0].id
        })
      } else {
        wx.showToast({
          title: res.data.data.message,
          icon: 'none',
          mask: true,
          duration: 4000,
        })
      }
    })
  }, 
  /* 获取推荐会员卡列表 */
  getList(){
    wx.$.fetch('api/cards',{
      hideLoading:false,
      method:'GET',
      data:{
        is_recommend: 1
      }
    }).then(res=>{
      console.log(res)
      this.setData({
        // content_html: res.data.data[0].desc,
        recommend:res.data.data
      })
    })
  },
  /* 获取会员卡列表 */
  getObtainCard() {
    wx.$.fetch('api/member/card/my', {
      method: 'get',
      data:{
        page:this.data.cardsListpage
      }
    }).then(res => {
      // console.log(res)
      let data = res.data.data;
      if (data.current_page <= data.last_page){
        let cardsList = this.data.cardsList.concat(data.data) 
        this.setData({
          cardsList: data.data,
          content_html: data.data[0].card.desc
        })
      }
      this.data.cardsListpage = data.current_page
    })
  },
  getSwiperitem(current) {
    console.log(current.detail.current)
    let index = current.detail.current + 1;
    if (index == this.data.cardsList.length){
      this.data.cardsListpage = this.data.cardsListpage + 1;
      this.getObtainCard(true)
      this.setData({
        content_html: this.data.cardsList[current.detail.current].card.desc
      })
    }
  },
  /* wxParse回调 */
  handleWxparseRendered: function () {
    setTimeout(() => {
      wx.hideLoading();
    }, 200);
    this.setData({
      loading: false
    });
  },
  /* 调起支付接口 */
  buthis(e) {
    console.log(e)
    let card_id = e.currentTarget.dataset.id;
    var _that = this
    const system = wx.getSystemInfoSync().system;
    if (system.indexOf("iOS") != -1) {
      var buyInfo = {
        goods_id: 3,
        goods_type: 'buy',
        quantity: 1
      };
      buyInfo = JSON.stringify(buyInfo);
      this.setData({
        buyInfo: buyInfo,
        showContact: true,
        ios: true
      })
    } else {
      wx.$.fetch(`api/order/checkout`, {
        method: 'post',
        data: {
          card_id: card_id,
          recommend_id: '',
          channel: '',
        }
      }).then(res => {
        console.log(res)
        if (res.data.code == 200) {
          wx.requestPayment({
            'timeStamp': res.data.data.timeStamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            'success': function (res) {
              wx.showToast({
                title: '购买成功'
              })
            }
          })
        }
      })
    }
  },
  /* 购买vip */
  purchaseVip(e) {
    let balance = e.currentTarget.dataset.balance
    this.setData({
      member_card_id: balance.id
    })
    if (balance.balance) {
      wx.navigateTo({
          url: "/pages/openingVip/openingVip"
        });
    } else {
      this.Sweepcode()
    }
  },
  purchaseVip2(e) {
    wx.navigateTo({
      url: "/pages/openingVip/openingVip"
    });
  },
  /* ios关闭提示 */
  closeContact() {
    this.setData({
      showContact: false
    })
  },
  noHandler() {
    return false
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: "pages/home/home",
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    };
  }
})